package com.example.parking.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.parking.dto.CarFrom;
import com.example.parking.dto.Result;
import com.example.parking.mapper.CarInfoMapper;
import com.example.parking.entity.CarInfo;
import com.example.parking.service.ICarInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/car")
@CrossOrigin//跨域
public class CarInfoController {

    @Resource
    private CarInfoMapper carInfoMapper;

    @Resource
    private ICarInfoService carInfoService;

    /**
     * 分页查询全部车辆信息
     * @return
     */
    @GetMapping("/getCar")
    public Result getCar(@RequestParam Integer pageNum,
                          @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        Page<CarInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<CarInfo> wrapper = new QueryWrapper<>();
        Page<CarInfo> carInfoPage = carInfoService.page(page, wrapper);
        return Result.successData(carInfoPage);
    }

    /**
     * 分页模糊查询
     * @param pageNum
     * @param pageSize
     * @param carNum
     * @return
     */
    @GetMapping("getCarByName")
    public Result getCarByName(@RequestParam(required = false,defaultValue = "1")Integer pageNum,
                            @RequestParam(required = false,defaultValue = "10")Integer pageSize,
                            @Param("carNum") String carNum){
        //配置分页规则
        Page<CarInfo> pageInfo = new Page<>(pageNum,pageSize);
        //进行参数筛选
        LambdaQueryWrapper<CarInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(carNum),CarInfo::getCarNum,carNum);
        Page<CarInfo> carInfoPage = carInfoService.page(pageInfo, wrapper);
        return Result.successData(carInfoPage);
    }

    /**
     * 根据id删除车辆信息(操作Database)
     * @param carId
     */
    @PostMapping("/deleteUser/{carId}")
    public Result deleteCar(@PathVariable Integer carId){
        carInfoService.removeById(carId);
        return Result.success();
    }


    /**
     * 更新用户信息(管理员操作)
     * @param carFrom
     * @return
     */
    @PostMapping("updateCar")
    public Result updateCar(@RequestBody CarFrom carFrom){
        CarInfo carInfo = BeanUtil.toBean(carFrom, CarInfo.class);
        if (carInfoService.updateById(carInfo)){
            return Result.success();
        }else {
            return Result.fail("更新失败");
        }
    }

    /**
     * 获取当前登录用户的车辆信息
     * @param userId
     * @return
     */
    @GetMapping("getLoginCar/{userId}")
    public Result getLoginCar(@PathVariable Integer userId){
        CarInfo carInfo = carInfoMapper.getByLogin(userId);
        CarFrom car = BeanUtil.copyProperties(carInfo, CarFrom.class);
        return Result.successData(car);
    }
}